package org.baitu.zzim.consumer;

import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.baitu.zzim.model.ChatMessage;
import org.baitu.zzim.service.ChannelManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangkai_a
 * @date 2025/7/9 11:07
 */
@Component
@RocketMQMessageListener(
        topic = "im-chat",
        consumerGroup = "im-Listener-group",
        messageModel = MessageModel.BROADCASTING
)
public class ChatMessageListener implements RocketMQListener<ChatMessage> {
    @Autowired
    private ChannelManager channelManager;

    @Override
    public void onMessage(ChatMessage message) {
        // 将消息投递给 receiver 用户
        if (channelManager.containsChannel(message.getReceiver())) {
            channelManager.sendToUser(message.getReceiver(), message);
        }
    }
}